package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.DictNameEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.KhjcRewardPunishmentConvert;
import com.engine4cloud.tdf.supervision.dto.create.KhjcRewardPunishmentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.KhjcRewardPunishmentQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.KhjcRewardPunishmentResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.KhjcRewardPunishmentUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxRewardPunishmentEntity;
import com.engine4cloud.tdf.supervision.mapper.KhjcRewardPunishmentMapper;
import com.engine4cloud.tdf.supervision.service.DictSelectService;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.KhjcRewardPunishmentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 矫正对象奖惩信息
 *
 * @author wenjie
 * @date 2024-05-09 15:53:10
 */
@Service
public class KhjcRewardPunishmentServiceImpl extends ServiceImpl<KhjcRewardPunishmentMapper, JzdxRewardPunishmentEntity> implements KhjcRewardPunishmentService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Resource
    private DictSelectService dictSelectService;

    @Override
    public PageInfo<KhjcRewardPunishmentResultDTO> pageList(Params pageable, KhjcRewardPunishmentQueryDTO dto) {
        return Pages.convert(this.baseMapper.pageList(Pages.page(pageable), dto));
    }

    @Override
    public KhjcRewardPunishmentResultDTO getById(Long id) {
        //设置奖惩类别
        List<DictSelectResultDTO> categoryList = dictSelectService.items(DictNameEnum.REWARD_PUNISHMENT_TYPE.getType());

        KhjcRewardPunishmentResultDTO result = Optional.ofNullable(KhjcRewardPunishmentConvert.INSTANCE.convert(super.getById(id), categoryList))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(KhjcRewardPunishmentCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        JzdxRewardPunishmentEntity entity = KhjcRewardPunishmentConvert.INSTANCE.create(dto, user.getTenantId());
        super.save(entity);
        //保存文件信息
        this.saveInstrumentList(dto, entity, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(KhjcRewardPunishmentUpdateDTO dto) {
        JzdxRewardPunishmentEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        super.updateById(KhjcRewardPunishmentConvert.INSTANCE.update(dto, user.getTenantId()));
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    private void saveInstrumentList(KhjcRewardPunishmentCreateDTO dto, JzdxRewardPunishmentEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_REWARD_PUNISHMENT), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(KhjcRewardPunishmentUpdateDTO dto, JzdxRewardPunishmentEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_REWARD_PUNISHMENT), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(KhjcRewardPunishmentResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_REWARD_PUNISHMENT);
        dto.setInstrumentList(fileService.findList(query));
    }
}